package com.controller;

import java.sql.Date;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.bean.Intel;
import com.service.IntelService;

@SuppressWarnings("serial")
@WebServlet("/UserServlet/*")
public class UserServlet extends AncestorServlet
{
	
	private final IntelService intelService = IntelService.getInstance();
	
	
	
	
	
	/**
	 * 用户端：查询全部帖子
	 */
	public String queryAllIntel(HttpServletRequest request)
	{
		List<Intel> intels = intelService.queryAllIntel();
		request.setAttribute("intels", intels);
		return "f:/list-every-intel.jsp";
	}
	
	
	
	
	/**
	 * 用户端：查询一个帖子
	 */
	public String queryOneIntel(HttpServletRequest request)
	{
		String id = request.getParameter("id");
		Intel intel = intelService.queryOneIntel(id);
		request.setAttribute("intel", intel);
		return "f:/show-one-intel.jsp";
	}
	
	
	
	
	
	/**
	 * 用户端：添加一个帖子
	 */
	public String insertOneIntel(HttpServletRequest request, HttpSession session, Intel entity)
	{
		entity.setPtime(new Date(System.currentTimeMillis()));
		intelService.insertOneIntel(entity);
		session.setAttribute("message", "添加成功");
		return "r:/user/add-one-intel.jsp";
		
	}
	
	
	
	
	
	/**
	 * 管理员端：删除一个我自己的帖子
	 */
	public Map<String,Object> deleteOneIntel(HttpServletRequest request, Map<String,Object> jsonMap)
	{
		String id = request.getParameter("id");
		intelService.deleteOneIntel(id);
		jsonMap.put("ok", true);
		return jsonMap;
	}
	
	
	
	
	
	/**
	 * 管理员端：编辑一个专业
	 */
	public String editOneIntel(HttpServletRequest request)
	{
		String id = request.getParameter("id");
		Intel intel = intelService.queryOneIntel(id);
		request.setAttribute("intel", intel);
		return "f:/jsp/intel/edit-one-intel.jsp";
	}
	
	
	
	
	
	/**
	 * 管理员端：修改一个专业
	 */
	/*public String updateOneIntel(HttpServletRequest request, Intel entity)
	{
		String id = request.getParameter("id");
		String sn = request.getParameter("sn");
		String name = request.getParameter("name");
		Intel intel = intelService.queryOneIntel(id);
		boolean bool = true;
		if(sn.length()==0 || sn.matches(".*\\s+.*"))  //检查字符串是否包含至少一个空白符
		{
			bool = false;
			request.setAttribute("sn", "编号不能为空,并且不能含有空白符");
		}
		if(!sn.equals(intel.getSn()) && intelService.existsIntelSn(sn))  //检查新的编号和原始编号是否相同,以及该编号是否已存在
		{
			bool = false;
			request.setAttribute("sn", "此编号已存在");
		}
		if(name.length()==0 || name.matches(".*\\s+.*"))
		{
			bool = false;
			request.setAttribute("name", "名称不能为空,并且不能含有空白符");
		}
		if(!name.equals(intel.getName()) && intelService.existsIntelName(name))  //检查新的名称和原始名称是否相同,以及该名称是否已存在
		{
			bool = false;
			request.setAttribute("name", "此名称已存在");
		}
		if(bool)
		{
			intelService.updateOneIntel(entity);
			request.setAttribute("intel", intelService.queryOneIntel(id));
			request.setAttribute("message", "修改成功");
		}
		else
		{
			request.setAttribute("intel", entity);
			request.setAttribute("message", "修改失败");
		}
		return "f:/jsp/intel/edit-one-intel.jsp";
	}*/
	
	
	
	
	
/**********************************************************************************************************************/

	
	
	
	
}
